package system;

import javax.swing.JTable;

import ui.MainMenuUI;
import ui.ResultsUI;

public class RandIndex {
	public static double[] rowSum; 
	public static double[] colSum;
	public static int[][] randMatrix;
	public static Object[] Groups; 
	public static Object[][] data;
	public static int groupsSize=0;
	public static double result=0;
	public double run(double[][] V, int number_of_docs, int number_of_clusters, int n)
	{
		double a=0,b=0,c=0,d=0;
		randMatrix=new int[n/number_of_docs][number_of_clusters];
		double max=0;
		int index=0;
		int number_of_books=n/number_of_docs;
		rowSum= new double[number_of_books];
		colSum =new double[number_of_clusters];
		//n=number of docs per book* number of books
		// number_of_docs per book
		
		int j=0;
		int k=0;
		double temp;
	    if(MainMenuUI.alg==1)
		MainMenuUI.lblRunStatus.setText("RAND INDEX EVALUETION IN PROGRESS...");
		
		for(int i=1;i<=number_of_books;i++){
					
		   for(;j<number_of_docs*i;j++){
			   if(MainMenuUI.alg==1){
			    temp=(i/number_of_books)*100.0;
				MainMenuUI.progressBar.setValue((int)temp);
				
				
				MainMenuUI.ChangeProgressBar();
	    		
			   }
			   for(k=0;k<number_of_clusters;k++){
				 //find max value that document j refers to
				   if(V[j][k]>max){
			 		 
			 		 max=V[j][k];
			 		 index=k;
			 	 }//if
			 	
			   }//for3
		      
			   randMatrix[i-1][index]++;
			   index=0;
			   max=0;
			   
		   
		   }//for2
		
		
		}//for1
	////////////////////////////////////////////////////
		NmfAlg.log=NmfAlg.log+("\n///////////////////////////////////////////////////////////////////////////////////////");
		NmfAlg.log=NmfAlg.log+("\n\nRAND INDEX RESULT: \n\n");
		NmfAlg.log=NmfAlg.log+("Clasification: \n\n");
		for(int i=0;i<number_of_books;i++){
			for(j=0;j<number_of_clusters;j++){
				NmfAlg.log=NmfAlg.log+(randMatrix[i][j]+" parts from "+"\""+MainMenuUI.RunningBooks.get(i).getTitel()+"\""+" book clsified to "+j+" cluster\n");
				//NmfAlg.log=NmfAlg.log+(randMatrix[i][j]+" ");
			}
		
			NmfAlg.log=NmfAlg.log+("-------------------------------------------------------------------------------------\n\n");
		}
		
	////////////////////////////////////////////////////


		
		
   ////////////////////////////////////////////////////
    //NmfAlg.log=NmfAlg.log+("ROW SUMS: \n");
	for(int i=0;i<number_of_books;i++){
		 for(j=0;j<number_of_clusters;j++){
			 
			 rowSum[i]=rowSum[i]+randMatrix[i][j];
		 }
		NmfAlg.log=NmfAlg.log+("total number of "+"\""+MainMenuUI.RunningBooks.get(i).getTitel()+"\""+" book is: "+rowSum[i]+"\n");
	}
	
	///////////////////////////////////////////////////
	//NmfAlg.log=NmfAlg.log+("\n\n\nCOLS SUMS: \n");
	for(int i=0;i<number_of_clusters;i++){
		 for(j=0;j<number_of_books;j++){
			 
			 colSum[i]=colSum[i]+randMatrix[j][i];
			
		 }
		// NmfAlg.log=NmfAlg.log+(colSum[i]+" ");
	}
	////////////////////////////////////////////////////
	for(int i=0;i<number_of_books;i++){
		for(j=0;j<number_of_clusters;j++){
			
				if(randMatrix[i][j]>=2)
					a=a+permutation(randMatrix[i][j],2);
		
				
		}
	
		
	}
	NmfAlg.log=NmfAlg.log+("\n\nA value: "+a+"\n");
	//////////////////////////////////////////////////////
	
	for(int i=0;i<number_of_books;i++){
		if(rowSum[i]>=2){
			rowSum[i]=permutation(rowSum[i],2);
			b=b+rowSum[i];
		}
	}
	b=b-a;
	NmfAlg.log=NmfAlg.log+("B value: "+b+"\n");
	
	///////////////////////////////////////////////////////
	
	for(int i=0;i<number_of_clusters;i++){
		if(colSum[i]>=2){
			//System.out.println("check");
			colSum[i]=permutation(colSum[i],2);
			c=c+colSum[i];
		}
	}
	c=c-a;
	NmfAlg.log=NmfAlg.log+("C value: "+c+"\n");
	
	////////////////////////////////////////////////////////

	d=permutation(n,2)-a-b-c;
	NmfAlg.log=NmfAlg.log+("D value: "+d+"\n");
	result=(double)((a+d)/(a+b+c+d));
	NmfAlg.log=NmfAlg.log+("\n RAND INDEX VALUE: (a+d)/(a+b+c+d): "+ result+"\n");
	
	if(Algorithm.bestRandIndexValue<result){
		Algorithm.bestRandMatrix=new double[number_of_books][number_of_clusters];
		for(int i=0;i<number_of_books;i++)
			for(int h=0;h<number_of_clusters;h++)
			{
				Algorithm.bestRandMatrix[i][h]=randMatrix[i][h];
			}
		Algorithm.bestRandIndexValue=result;
	
	
	Groups = new String[number_of_clusters+1];
	groupsSize=number_of_clusters+1;
	Groups[0]="Books/Groups";
	for(int i=1;i<number_of_clusters+1;i++)
	{
		Groups[i]="Group "+(i);
		System.out.println(Groups[i]);
	}
	
	data=new Object[number_of_books][number_of_clusters+1];
	for(int i=0;i<number_of_books;i++){
		for(j=0;j<number_of_clusters+1;j++)
		{
			if(j==0)
				data[i][j]=MainMenuUI.RunningBooks.get(i).getTitel();
			else
			{
				data[i][j]=Algorithm.bestRandMatrix[i][j-1];
			}
		}
	}
	
	}
	
	NmfAlg.log=NmfAlg.log+("\n///////////////////////////////////////////////////////////////////////////////////////");
	
	MainMenuUI.lblRunStatus.setText("RAND INDEX EVALUETION COMPLITED!");
	 return result;
	}
   

	
	public double permutation(double n,int k){
		double res=1;
		
		for(int i=0;i<=(k-1);i++)
		{
			res=res*(n-i);
			
		}
		
		
	return res;
		
	}
	

}
